﻿using Mengsk.ProxyDelivery.Domain;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Mengsk.ProxyDelivery.Service.Delivery
{
    class YTOCodComputer : IDeliveryMoneyComputer
    {
        string[] citys = new string[] { "新疆", "西藏", "内蒙古", "海南" };

        public bool Accept(OrderPopPayType payType, string deliveryCompany)
        {
            return payType == OrderPopPayType.COD && deliveryCompany.Contains("圆通");
        }

        public float ComputeMoney(OrderPopPayType payType, string deliveryCompany, double weight, string receiverAddress)
        {
            if (weight <= 1.3)
            {
                return 6F;
            }

            var pro = AddressService.ParseProvince(receiverAddress);
            if (pro == null)
            {
                throw new Exception("没有地址中的省:" + receiverAddress);
            }

            float stepPrice = citys.Any(obj => pro.Name.Contains(obj)) ? 2F : 1.5F;

            if (weight <= 1.5F)
            {
                return 6F + (float)Math.Round((stepPrice / 2), 1);
            }

            int step = 500;
            int stepCount = ((int)(weight * 100) + step - 1 - 1000) / step;
            if (stepCount < 0)
            {
                stepCount = 0;
            }

            if (step % 2 == 0)
            {
                return 6F + stepPrice * stepCount;
            }

            return 6F + stepPrice * (stepCount - 1) + (float)Math.Round((stepPrice / 2), 1);
        }
    }
}
